<?php
//TODO:提交动作完成后的转向
class PostController extends BaseController
{

	public function actionIndex(){
		
		echo 'post index';
		//$this->display('default_index.tpl');
	}
	
	//增加试卷界面
	public function actionVol(){
		
		//echo 'post vol';
		$this->display('post_vol.tpl');
	}
	
	//增加试卷的处理
	public function actionVol_op(){
		if(empty($_POST['op']) || empty($_POST['intro'])) return false;
		
		$title = trim($_POST['title']);
		$item = $_POST['item'];
		$intro = trim($_POST['intro']);
		$time = empty($_POST['time'])? time() : strtotime($_POST['time']);
		$tags = $item . ',' . trim($_POST['tags']);
		
		
		$uid = obj('User')->is_login();
		$re = obj('User')->get_user($uid);
		@$user_name = empty($re['name'])?'':$re['name'];
		//题目的处理
		$ti_arr = $_POST['ti'];
		$tis = '|'; //题目id用|相连,首尾都有|  查找用"|$id|" 删除用"|"替换"|$id|"
		foreach($ti_arr as $k=>$v) {
			if(empty($v)) continue;
			$ti_id = obj('Ti')->add_one($v,$time);
			$tis .= $ti_id . '|';
		}
		
		$id = obj('Vol')->add($title,$item,$intro,$tis,$time,$tags,$uid,$user_name);
		
		
		//标签的处理
		obj('Tag2vol')->add_tags2vol($tags,$id);
		
		
		echo obj('Vol')->sql;
		
		var_dump($_POST);
		// var_dump($tag_arr);
		echo 'op post vol';
		// $this->display('post_vol.tpl');
	}
	
	
	//TODO:可以修改合并题目
	public function actionTi(){
		
		// echo 'post ti';
		$this->display('post_ti.tpl');
	}
	
	
	//题回复的处理  试卷回复处理另外做
	//TODO: 权限检测   和垃圾过滤
	public function actionRe_ti() {
		// var_dump($_POST);
		//TODO: user_id  user_name 待处理 
		
		$uid = obj('User')->is_login();
		$re = obj('User')->get_user($uid);
		@$user_name = $re['name'];
		
		$content = $_POST['re_content'];
		$ti_id = (int) $_POST['ti_id'];
		if(empty($uid)|| ''==trim($content)) {header('Location:'.URL . "ti/$ti_id.html");return;}//直接回到题目去
		
		
		$time = time();
		obj('Ti_re')->insert(array('ti_id'=>$ti_id,'cr_time'=>$time,'content'=>$content,'user_id'=>$uid,'user_name'=>$user_name));
		header('Location:'.URL . "ti/$ti_id.html");//直接回到题目去
	}
	
}